//
//  TMLoginController.m
//  国学商家管理
//
//  Created by mac on 2018/6/19.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMLoginController.h"
#import "TMThridLoginView.h"
#import "TMPhoneLoginController.h"
#import "TMFindOrRegiserController.h"
//#import "TMApproveController.h"
#import "TMShareManager.h"
#import "UIImageView+WebCache.h"
#import <UMShare/UMShare.h>
#import "STWebViewController.h"
@interface TMLoginController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *accounTf;
@property (weak, nonatomic) IBOutlet UITextField *passwordTf;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConst;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconConst;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textfildConst;
@property (weak, nonatomic) IBOutlet TMThridLoginView *thridLoninView;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageV;
@property (nonatomic, assign) NSInteger hasCheck;
@end

@implementation TMLoginController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    self.loginBtn.layer.cornerRadius = 8.0f;
    self.loginBtn.layer.masksToBounds = YES;
    self.accounTf.delegate = self;
    self.passwordTf.delegate = self;
    if (kWindowW == 320) {
        self.iconConst.constant = 20;
        self.textfildConst.constant = 20;
    }
    if (kWindowH == 812) {
        self.heightConst.constant = -44;
    }
    [self displayThridLoginView];
    

    
}
- (IBAction)onSelctedYouke:(id)sender {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    TMBaseTabbarContriller *rootTabbar = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"TMBaseTabbarContriller"];
    window.rootViewController = rootTabbar;
}
- (IBAction)onSelctedPrivate:(id)sender {
    NSString * url =@"http://shishi2017.com/home/web/odds_rule_web";
    STWebViewController * vc = [[STWebViewController alloc] initWithUrl:url];
    vc.forceTitle = @"隐私政策";
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)displayThridLoginView{
    
    self.thridLoninView.qqBlock = ^{
        [[TMShareManager defaultManager] loginWithQQSuccess:^(id respone){
            UMSocialUserInfoResponse *resp = respone;
            NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"registrationID"];
            if (!str.length) {
                str = @"";
            }
            //            NSDictionary *dic = @{@"phone":userName,@"password":password,@"type":@"password_login",@"device":str};
            NSDictionary *dic = @{@"openid":resp.openid,@"userimage": resp.iconurl,@"nickname":resp.name,@"types":@(2),@"device": str};
            [TMUserManager loginWithWecatWith:dic Successed:^(id resppp) {
                if (![TMUserManager deaufatManager].userModel.uid) {
                    TMPhoneLoginController *phone = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMPhoneLoginController"];
                    phone.isblind = YES;
                    phone.openid = resp.openid;
                    [self.navigationController pushViewController:phone animated:YES];
                }else{
                    UIWindow *window = [UIApplication sharedApplication].keyWindow;
                    TMBaseTabbarContriller *rootTabbar = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"TMBaseTabbarContriller"];
                    window.rootViewController = rootTabbar;
                }
            }];
        }];
    };
    
    self.thridLoninView.wbBlock = ^{
       // [[TMShareManager defaultManager] loginWithWB];
    };
    
    self.thridLoninView.wxBlock = ^{
        [[TMShareManager defaultManager] loginWithWXSuccess:^(id respone){
            UMSocialUserInfoResponse *resp = respone;
            NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"registrationID"];
            if (!str.length) {
                str = @"";
            }
//            NSDictionary *dic = @{@"phone":userName,@"password":password,@"type":@"password_login",@"device":str};
           NSDictionary *dic = @{@"openid":resp.openid,@"userimage": resp.iconurl,@"nickname":resp.name,@"types":@(1),@"device": str};
            [TMUserManager loginWithWecatWith:dic Successed:^(id resppp) {
                if (![TMUserManager deaufatManager].userModel.uid) {
                    TMPhoneLoginController *phone = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMPhoneLoginController"];
                    phone.isblind = YES;
                    phone.openid = resp.openid;
                    [self.navigationController pushViewController:phone animated:YES];
                }else{
                    UIWindow *window = [UIApplication sharedApplication].keyWindow;
                    TMBaseTabbarContriller *rootTabbar = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"TMBaseTabbarContriller"];
                    window.rootViewController = rootTabbar;
                }
            }];
        }];
    };
}


- (IBAction)gotoLoginMethod:(UIButton *)sender {
    TMFindOrRegiserController *registerVC = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMFindOrRegiserController"];
    registerVC.isRegisterVC = YES;
    [self.navigationController pushViewController:registerVC animated:YES];
}
- (IBAction)phoneLoginMehtod:(UIButton *)sender {
    TMPhoneLoginController *phone = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMPhoneLoginController"];
    [self.navigationController pushViewController:phone animated:YES];
}

- (IBAction)forgatePasswordMohted:(UIButton *)sender {
    TMFindOrRegiserController *registerVC = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMFindOrRegiserController"];
    registerVC.isRegisterVC = NO;
    [self.navigationController pushViewController:registerVC animated:YES];
}
- (IBAction)loginBtnMethod:(UIButton *)sender {
    // @"15681168721"
    if (![SPToolSet isMobileNumber:self.accounTf.text]) {
        [TMProgressHUD showErrorWithStyleDarkStatus:@"请输入正确的手机号！"];
        return;
    }
    if (self.passwordTf.text.length == 0 || !self.passwordTf.text) {
        [TMProgressHUD showErrorWithStyleDarkStatus:@"请输入密码！"];
        return;
    }
    if (![SPToolSet checkPassword:self.passwordTf.text]) {
        [TMProgressHUD showErrorWithStyleDarkStatus:@"请输入6-18位字母数字组合密码！"];
        return;
    }
    [TMUserManager loginWithUserName:self.accounTf.text  password:self.passwordTf.text successed:^(id index){
        [TMUserManager displayRCConfings];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        TMBaseTabbarContriller *rootTabbar = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"TMBaseTabbarContriller"];
        window.rootViewController = rootTabbar;
        [JPUSHService addTags:[NSSet setWithObject:@([TMUserManager deaufatManager].userModel.uid).description] completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
            
        } seq:1];
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.accounTf) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.accounTf.text.length >= 11) {
            self.accounTf.text = [textField.text substringToIndex:11];
            return NO;
        }
    }
    
    if (textField == self.passwordTf) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.passwordTf.text.length >= 18 ) {
            self.passwordTf.text = [textField.text substringToIndex:18];
            return NO;
        }
    }
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([textField isFirstResponder]) {
        [textField resignFirstResponder];
    }
    return YES;
}

@end
